#include "Tool.h"
#include "Game.h"
#include "ObjectDatabase.h"
#include "StringLib.h"
#include "Renderer.h"
#include "Util.h"

Tool::Tool(ToolType type, ToolPrefix prefix, ToolSuffix suffix, bool highQuality) : EquippableItem(GT_IT_TOOL) {
	m_types[TH_TOOL_TYPE] = type;
	const ToolInfo& info = g_objectDatabase->getToolInfo(type);
	m_prefix = prefix;
	m_suffix = suffix;
	m_weight = info.weight;
	m_highQuality = highQuality;
	m_defaultPower = 100;
	m_defaultSpeed = int(m_prefix)*5+5;
	
	m_power = m_defaultPower;
	m_speed = m_defaultSpeed;

	//switch(suffix) {
	//}

	if(highQuality) {
		m_power += 5;
		m_speed -= 5;
	}
}

Tool::Tool(std::ifstream& file) : EquippableItem(GT_IT_TOOL, file) {
	int* tokens = Util::intSplit(file);
	m_types[TH_TOOL_TYPE] = tokens[0];
	m_prefix = ToolPrefix(tokens[1]);
	m_suffix = ToolSuffix(tokens[2]);
	m_highQuality = tokens[3] == 1;
	m_defaultPower = 100;
	m_defaultSpeed = int(m_prefix)*5+5;
	m_power = tokens[4];
	m_speed = tokens[5];
	delete[] tokens;
}

Tool::~Tool() {
}

void Tool::save(std::ofstream& file) const {
	file << getType(TH_GENERIC_TYPE) << "\n";
	EquippableItem::save(file);
	file << getType(TH_TOOL_TYPE) << m_prefix << " " << m_suffix << " " << (m_highQuality ? 1 : 0) << " " << m_power << " " << m_speed << "\n";
}

void Tool::render(float x, float y) {
	g_renderer->render(*this, x, y);
}

std::string Tool::getItemName(int mod) const {
	std::string output = "";
	if(m_statusIdentified)
		output += g_stringLib->getMiscString(MST_STATUS, m_status) + " ";

	if(m_equippableIdentified) {
		if(m_highQuality)
			output += "high quality ";
		if(m_prefix != TP_NONE)
			output += g_stringLib->getMiscString(MST_TOOL_PREFIX, m_prefix) + " ";
	} else {
		if(isEnchanted())
			output += "enchanted ";
	}
	output += getBasicName();
	if(m_equippableIdentified && m_suffix)
		output += " " + g_stringLib->getMiscString(MST_TOOL_SUFFIX, m_suffix);
	if(m_equippableIdentified)
		output += " (" + Util::intToString(m_power) + ", " + Util::intToString(m_speed) + "%)";
	else
		output += " (" + Util::intToString(m_defaultPower) + ", " + Util::intToString(m_defaultSpeed) + "%)";
	return output;
}

bool Tool::isEnchanted() const {
	return m_suffix != TS_OF_NONE || m_highQuality;
}


void Tool::logDetails(TextLog& log) const {
	Item::logDetails(log);
	log.addBlankLine();
	if(m_equippableIdentified)
		log.addLine(g_stringLib->getTextString(ST_TOOL_STATS, (Util::intToString(m_power).c_str(), Util::intToString(m_speed).c_str())));
	else
		log.addLine(g_stringLib->getTextString(ST_TOOL_STATS, (Util::intToString(m_defaultPower).c_str(), Util::intToString(m_defaultSpeed).c_str())));
	log.addBlankLine();
	if(m_equippableIdentified) {
		if(m_highQuality)
			log.addLine(g_stringLib->getTextString(ST_TOOL_HIGH_QUALITY));
		if(m_prefix != TP_NONE)
			log.addLine(g_stringLib->getMiscString(MST_TOOL_PREFIX, m_prefix));
		if(m_suffix != TS_OF_NONE)
			log.addLine(g_stringLib->getMiscString(MST_TOOL_SUFFIX, m_suffix));
	}
}
